package task3

import static org.junit.Assert.*
import org.junit.*

class OrganizationControllerIntegrationTests {
    def organizationService

    @Test
    void testGetNodeInDatabase() {

        def organizationController = new OrganizationController()
        organizationController.organizationService = organizationService

        def dep = new Department(name: "Отдел1")
        dep.save()
        def proj = new Project(name: "Проект1", department: dep)
        proj.save()
        def hum = new Human(name: "Sun Wain", position: "tester", project: proj)
        hum.save()

        organizationController.params.id = organizationService.HUMAN_ID_PREFIX + hum.getId()
        def objHum = organizationService.getNodeInDatabase(organizationController.params)

        organizationController.params.id = organizationService.PROJECT_ID_PREFIX + proj.getId()
        def objProj = organizationService.getNodeInDatabase(organizationController.params)

        organizationController.params.id = organizationService.DEPARTMENT_ID_PREFIX + dep.getId()
        def objDep = organizationService.getNodeInDatabase(organizationController.params)

        organizationController.params.id = organizationService.ORGANIZATION_ID_PREFIX + 1
        def nullObj = organizationService.getNodeInDatabase(organizationController.params)

        organizationController.params.id = organizationService.HUMAN_ID_PREFIX + 4000
        def objHumNull = organizationService.getNodeInDatabase(organizationController.params)

        assertEquals dep, objDep
        assertEquals proj, objProj
        assertEquals hum, objHum
        assertNull nullObj
        assertNull objHumNull

    }

    @Test
    void testDeleteNode() {
        def organizationController = new OrganizationController()
        organizationController.organizationService = organizationService

        def dep = new Department(name: "Отдел1")
        dep.save()
        def proj = new Project(name: "Проект1", department: dep)
        proj.save()
        def hum = new Human(name: "Sun Wain", position: "tester", project: proj)
        hum.save()

        organizationController.params.id = organizationService.HUMAN_ID_PREFIX + hum.getId()
        organizationController.jsonDeleteNode()

        assertEquals "{"success":true}", organizationController.response.text
    }

    @Test
    void testUpdateNodeName() {
        def organizationController = new OrganizationController()
        organizationController.organizationService = organizationService

        def dep = new Department(name: "Отдел1")
        dep.save()
        def proj = new Project(name: "Проект1", department: dep)
        proj.save()
        def hum = new Human(name: "Sun Wain", position: "tester", project: proj)
        hum.save()
        def humFound

        organizationController.params.id = organizationService.HUMAN_ID_PREFIX + hum.getId()
        organizationController.params.newName = "Vasya Pupov"

        organizationController.jsonUpdateNodeName()
        assertEquals true, organizationController.response.json.success
        humFound = Human.get(hum.id)
        assertEquals humFound.name, "Vasya Pupov"


        organizationController.params.id = organizationService.HUMAN_ID_PREFIX + hum.getId()
        organizationController.params.newName = "1"
        organizationController.jsonUpdateNodeName()

        assertFalse hum.validate()
        assertTrue hum.hasErrors()

        hum.errors.allErrors.each {println it}
        humFound = Human.get(hum.id)
        assertEquals humFound.name, "Vasya Pupov"
//        assertEquals false, organizationController.response.json.success
    }

    @Test
    void testSaveDataFromForm() {
        def organizationController = new OrganizationController()
        organizationController.organizationService = organizationService

        def dep = new Department(name: "Отдел1")
        dep.save()

        def paramsProj = [:]
        organizationController.params.name="Проект1"
        organizationController.params.parentId=organizationService.DEPARTMENT_ID_PREFIX+dep.id
        organizationController.params.id=null

        organizationController.saveDataFromForm()

        assertEquals true, organizationController.response.json.success

        def projFound=Project.findAllByNameLike("%роект%")[0]

        organizationController.params.name="Проект666"
        organizationController.params.id=organizationService.PROJECT_ID_PREFIX+projFound.id

        organizationController.saveDataFromForm()

        assertEquals true, organizationController.response.json.success

        def projConfirm =Project.get(projFound.id)
        assertEquals projConfirm.name,"Проект666"

        organizationController.params.name=""
        organizationController.params.id=organizationService.PROJECT_ID_PREFIX+projFound.id

        organizationController.saveDataFromForm()
        assertFalse projFound.validate()
//        assertEquals "blank",projFound.errors["name"]

        projConfirm =Project.get(projFound.id)
        assertEquals projConfirm.name,"Проект666"

//        assertEquals false, organizationController.response.json.success
//        assertEquals "blank", organizationController.response.json.errors.name

    }


}
